# -*- coding: utf-8 -*-
# !/usr/bin/env python
from rest_framework.views import APIView
from components.kubernetes.api_file_verify import get_info
from utils.K8S import K8sApi
from utils.views import Result, ReturnClass
from copy import deepcopy


class CompareImageVersion(APIView):
    def get(self, request):
        data = request.query_params
        old_kubernetes_cluster_id = data['old_kubernetes_cluster_id']
        now_kubernetes_cluster_id = data['now_kubernetes_cluster_id']
        old_version_cluster_select_name = data['old_version_cluster_select_name']
        now_version_cluster_select_name = data['now_version_cluster_select_name']

        old_return_info = get_info(old_kubernetes_cluster_id)
        if ReturnClass.if_ok(old_return_info):
            old_kubernetes_cluster_sign_kubeconfig = ReturnClass.get_value(old_return_info)
        else:
            return Result.fail_response(ReturnClass.get_value(old_return_info))

        now_return_info = get_info(now_kubernetes_cluster_id)
        if ReturnClass.if_ok(now_return_info):
            now_kubernetes_cluster_sign_kubeconfig = ReturnClass.get_value(now_return_info)
        else:
            return Result.fail_response(ReturnClass.get_value(now_return_info))

        now_k8s_api = K8sApi(now_kubernetes_cluster_sign_kubeconfig)
        old_k8s_api = K8sApi(old_kubernetes_cluster_sign_kubeconfig)

        try:
            now_data = now_k8s_api.get_namespaced_deployment(now_version_cluster_select_name)
            old_data = old_k8s_api.get_namespaced_deployment(old_version_cluster_select_name)


            data_one_containers = [[], []]

            for re_index, re_data in enumerate([now_data, old_data]):
                for re_data_one in re_data:
                    re_data_one_swap = deepcopy(re_data_one)
                    re_data_one_swap.pop("containers")
                    for containers_one in re_data_one['containers']:
                        re_data_one_swap['containers_name'] = containers_one['name']
                        re_data_one_swap['image'] = containers_one['image']
                    data_one_containers[re_index].append(re_data_one_swap)

            now_data_one_containers = data_one_containers[0]
            old_data_one_containers = data_one_containers[1]

            return_data = []
            for now_data_one_containers_one in now_data_one_containers:
                for old_data_one_containers_one in old_data_one_containers:

                    if now_data_one_containers_one['image'] != old_data_one_containers_one['image']:
                        tar_version_pod_image = now_data_one_containers_one['image']
                    else:
                        tar_version_pod_image = ""

                    if now_data_one_containers_one['name'] == old_data_one_containers_one['name'] and \
                            now_data_one_containers_one['containers_name'] == old_data_one_containers_one[
                        'containers_name']:
                        return_data.append({
                            "id": now_data_one_containers_one['name'],
                            "now_version_pod_name": now_data_one_containers_one['name'],
                            "now_version_pod_image": now_data_one_containers_one['image'],
                            "old_version_pod_name": old_data_one_containers_one['name'],
                            "old_version_pod_image": old_data_one_containers_one['image'],
                            "tar_version_pod_image": tar_version_pod_image,
                            "children": [{
                                    "id": 'children-%s' %now_data_one_containers_one['name'],
                                    "now_image_repository": now_data_one_containers_one['image'].split("/")[0],
                                    "now_pod_image_version": now_data_one_containers_one['image'].split(":")[-1],
                                    "old_image_repository": old_data_one_containers_one['image'].split("/")[0],
                                    "old_pod_image_version": old_data_one_containers_one['image'].split(":")[-1],
                                    "is_children": 1,
                                }]
                        })


        except Exception as e:
            return Result.fail_response(msg=str(e))
        return Result.success_response(data=return_data, msg='执行成功')
